package com.hznu.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hznu.commonutils.MD5;
import com.hznu.commonutils.handler.CustomException;
import com.hznu.ucenter.entity.Ucenter;
import com.hznu.ucenter.entity.vo.RegisterVo;
import com.hznu.ucenter.mapper.UcenterMapper;
import com.hznu.ucenter.service.UcenterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdd
 * @since 2021-03-10
 */
@Service
public class UcenterServiceImpl extends ServiceImpl<UcenterMapper, Ucenter> implements UcenterService {

    /**
     * 用户注册的具体实现
     * @param registerVo
     */
    @Override
    public void insertUser(RegisterVo registerVo) {
        Ucenter ucenter = new Ucenter();
        // 注册用户的操作
        ucenter.setName(registerVo.getName());
        ucenter.setTelephone(registerVo.getTelephone());
        ucenter.setEmail(registerVo.getEmail());
        // 用户密码MD5加密
        ucenter.setPassword(MD5.encrypt(registerVo.getPassword()));

        // 判断手机或邮箱是否已被注册
        QueryWrapper<Ucenter> wrapperForTelephone = new QueryWrapper();
        QueryWrapper<Ucenter> wrapperForEmail = new QueryWrapper();
        wrapperForTelephone.eq("telephone",registerVo.getTelephone());
        wrapperForEmail.eq("email",registerVo.getEmail());
        if(baseMapper.selectOne(wrapperForTelephone) != null || baseMapper.selectOne(wrapperForEmail) != null) {
            throw new CustomException(20001, "手机号已存在或邮箱已存在");
        }
        // 判断内容是否为空
        if(StringUtils.isEmpty(registerVo.getName())) {
            throw new CustomException(20001, "用户名为空！");
        } else if(StringUtils.isEmpty(registerVo.getTelephone())) {
            throw new CustomException(20001, "手机为空！");
        } else if(StringUtils.isEmpty(registerVo.getEmail())) {
            throw new CustomException(20001, "邮箱为空！");
        } else if(StringUtils.isEmpty(registerVo.getPassword())) {
            throw new CustomException(20001, "密码为空！");
        } else{
            this.save(ucenter);
        }
    }

    /**
     * 用户登录的加工
     * @param phoneOrEmail
     * @param password
     * @return
     */
    @Override
    public Long login(String phoneOrEmail, String password) {
        // 手机登录或邮箱登录
        QueryWrapper<Ucenter> wrapperForTelephone = new QueryWrapper<>();
//        QueryWrapper<Ucenter> wrapperForEmail = new QueryWrapper<>();
        wrapperForTelephone.eq("telephone",phoneOrEmail);
        // 密码需要加密之后与数据库中的密码比较
        wrapperForTelephone.eq("password",MD5.encrypt(password));
//        wrapperForEmail.eq("email",phoneOrEmail);
//        wrapperForEmail.eq("password",MD5.encrypt(password));
        Ucenter ucenter;
        if(((ucenter = baseMapper.selectOne(wrapperForTelephone)) != null)
//                || ((ucenter = baseMapper.selectOne(wrapperForEmail)) != null)
        ) {
            return ucenter.getUserId();
        } else {
            throw new CustomException(20001, "登录失败！");
        }
    }
}
